package com.fingard.dsp.bank.directbank.yzb01;

import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * @Author Green
 * */
public class YZB01SinglePay extends YZB01Base implements ISinglePay  {

    @Override
    /** 单笔支付*/
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        detail.reqSeqID = p_req1908.singleDetail.askNumber;


        XmlTextWriter xmlWriter =  new XmlTextWriter();
        YZB01Header header =  new YZB01Header();
        String opName ;
        //同行 完成| 跨行
        if(notSameBank.equals(p_req1908.singleDetail.sameBnk)){
            opName = "B2ECrossBankTransfer";
        }
        else{
            if(isPrivate.equals(p_req1908.singleDetail.isPrivate)){
                opName = "B2EFeeExpense";
            }
            else{
                opName = "B2EBankInnerTransfer";
            }
        }
        header.transCode = opName;
        header.reqSeqNo = p_req1908.singleDetail.askNumber;
        xmlWriter.autoFormat = false;
        writeCommonHead(xmlWriter,header);
        XElement tmpElement  = new XElement("Body","");
        if(notSameBank.equals(p_req1908.singleDetail.sameBnk)){
            tmpElement.addChild(new XElement("Channel",""));
            tmpElement.addChild(new XElement("PayeeBankCode",p_req1908.singleDetail.cnapsCode));
        }

        tmpElement.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));
        tmpElement.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));
        tmpElement.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));
        tmpElement.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));
        String currency = convertCurToBank(p_req1908.singleDetail.payCur);
        tmpElement.addChild(new XElement("Currency",currency));
        tmpElement.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));
        tmpElement.addChild(new XElement("Remark",p_req1908.singleDetail.abstractATS));
        nodeAnalysis(p_req1908.singleDetail.srcXElement,tmpElement,getCharset());
        xmlWriter.writeXElement(tmpElement);
        xmlWriter.writeEndDocument();

        String[] retStrArr = sendToBank(xmlWriter.toXmlString(),opName);

        if(retStrArr[0].length() == 0){
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

            if(tmpBkReader != null){
                String tmpRetCode = tmpBkReader.getFirstText("/Root/Head/OpRetCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/Root/Head/OpRetMsg");
                if(BANK_RET_RIGHT_CODE.equals(tmpRetCode)){
                    detail.respCode = "0";
                    detail.respInfo = tmpRetInfo;
                    detail.payInfoCode = tmpRetCode;
                    detail.transState = tmpRetCode;
                    detail.transAmt = p_req1908.singleDetail.payAmount;
                    retAts.writeRetDetail(detail);
                }
                else{
                    retAts.writeStart();
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRetInfo);
                }
            }
            else{
                retAts.writeStart();
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpSbMsg.toString());
            }
        }
        else{
            retAts.writeStart();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(retStrArr[1].toString());
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    /**
     * 支付状态查询
     * */
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();

        String reqSeqID = p_req1944.askNumber;

        String opName = "B2ETrsResultQry";
        XmlTextWriter xmlWriter =  new XmlTextWriter();
        YZB01Header header =  new YZB01Header();
        header.transCode = opName;
        header.reqSeqNo = p_req1944.reqHeader.transSeqID;
        xmlWriter.autoFormat = false;
        writeCommonHead(xmlWriter,header);
        XElement tmpElement  = new XElement("Body","");
        tmpElement.addChild(new XElement("OpJnlNo",reqSeqID));
        tmpElement.addChild(new XElement("BeginDate",""));
        tmpElement.addChild(new XElement("EndDate",""));
        tmpElement.addChild(new XElement("TransCode",""));
        tmpElement.addChild(new XElement("JnlState",""));
        tmpElement.addChild(new XElement("pageSize","10"));
        tmpElement.addChild(new XElement("currentIndex","0"));

        xmlWriter.writeXElement(tmpElement);
        xmlWriter.writeEndDocument();

        String[] retStrArr = sendToBank(xmlWriter.toXmlString(),opName);
        if(retStrArr[0].length() == 0){
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

            if(tmpBkReader != null){
                String tmpRetCode = tmpBkReader.getFirstText("/Root/Head/OpRetCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/Root/Head/OpRetMsg");
                if(BANK_RET_RIGHT_CODE.equals(tmpRetCode)){
                    detail.respCode = "0";
                    detail.respInfo = tmpRetInfo;
                    detail.payInfoCode = tmpRetCode +"+"+ tmpBkReader.getFirstText("/Root/Body/List/Record/JnlState");
                    detail.reqSeqID = p_req1944.reqHeader.reqSeqID;
                    detail.transState = tmpRetCode +"+"+ tmpBkReader.getFirstText("/Root/Body/List/Record/JnlState");
                    detail.transAmt = tmpBkReader.getFirstText("/Root/Head/List/Record/Amount");
                    retAts.writeRetDetail(detail);
                }
                else{
                    detail.respCode = "-1";
                    detail.respInfo = tmpRetInfo;
                    retAts.writeRetDetail(detail);
                }
            }
            else{
                detail.respCode = "-1";
                detail.respInfo = tmpSbMsg.toString();
                retAts.writeRetDetail(detail);
            }
        }
        else{
            detail.respCode = "-1";
            detail.respInfo = retStrArr[1].toString();
            retAts.writeRetDetail(detail);
        }
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
